package com.bandit.controller.back;

import com.bandit.pojo.IPCount;
import com.bandit.pojo.User;
import com.bandit.service.UserService;
import com.bandit.utils.LayResult;
import com.bandit.utils.PageUtils;
import com.bandit.utils.RedisUtil;
import com.bandit.utils.ShiroUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/back/user")
public class UserController {

    private static final String prefix = "back/user";

    private static final List<String> permissionList = new ArrayList<>();

    static {
        permissionList.add(ShiroUtils.ADMIN_TAG);
        permissionList.add(ShiroUtils.NORMAL_TAG);
        permissionList.add(ShiroUtils.GUEST_TAG);
    }

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    UserService userService;

    @RequiresPermissions("back:user:list")
    @GetMapping("/list")
    public String toList(Model model){
        model.addAttribute("permissionList",permissionList);
        return prefix + "/list";
    }

    @RequiresPermissions("back:user:edit")
    @GetMapping("/edit")
    public String toEdit(Model model){
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        model.addAttribute("user",user);
        model.addAttribute("permissionList",permissionList);
        return prefix + "/edit";
    }

    @RequiresPermissions("back:user:add")
    @GetMapping("/add")
    public String toAdd(Model model){
        model.addAttribute("permissionList",permissionList);
        return prefix + "/add";
    }

    @RequiresPermissions("back:user:visitor")
    @GetMapping("/visitor")
    public String toVisitor(Model model){
        return prefix + "/visitor";
    }


    @RequiresPermissions("back:user:visitor")
    @GetMapping("/visitorQuery")
    @ResponseBody
    public LayResult visitorQuery(@RequestParam("page")int page,@RequestParam("limit")int limit){
        Set<String> keys = redisTemplate.keys("*");
        List<IPCount> ipCountList = new ArrayList<>();
        if (keys!=null){
            for (String key : keys) {
                String[] split = key.split(":");
                int count = (int) redisUtil.get(key);
                IPCount ipCount = new IPCount(count, split[0], split[1]);
                ipCountList.add(ipCount);
            }
        }
        //分页数据
        PageUtils pageUtils = new PageUtils(ipCountList, page, limit);
        List records = pageUtils.getRecords();
        //分页结束
        System.out.println(records);
        LayResult layResult = new LayResult(ipCountList.size());
        return layResult.put(LayResult.DATA_TAG,records);
    }


    @RequiresPermissions("back:user:delete")
    @GetMapping("/delete")
    @ResponseBody
    public LayResult delete(@RequestParam("id") int id){
        int update = userService.deleteUserByID(id);
        if (update == 0){
            return new LayResult(500);
        }else return new LayResult();
    }

    @RequiresPermissions("back:user:delete")
    @PostMapping("/deleteByIDS")
    @ResponseBody
    public LayResult deleteByIDS(String[] ids){
        for (String id : ids) {
            System.out.println(id);
        }
        int update = userService.deleteUserByIDS(ids);
        if (update == 0){
            return new LayResult(500);
        }else return new LayResult();
    }

    //旧密码验证
    @PostMapping("/oldPwd")
    @ResponseBody
    public LayResult pwdModify(@RequestParam("password") String password){
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        LayResult layResult = new LayResult();
        if(user.getPassword().equals(password)){
            return layResult.put(LayResult.DATA_TAG,"correct");
        }else return layResult.put(LayResult.DATA_TAG,"error");
    }

    @PostMapping("/perEdit")
    @ResponseBody
    public LayResult perEditByID(@RequestParam("id") int id,@RequestParam("permission") String permission){
        User user = userService.getUserByID(id);
        user.setPermission(permission);
        int update = userService.updateUser(user);
        if(update == 0){
            System.out.println("插入失败");
            return new LayResult(500);
        }else return new LayResult();
    }

    @GetMapping("/query")
    @ResponseBody
    public LayResult selectList(@RequestParam("page")int page,@RequestParam("limit")int limit){
        List<User> userList = userService.getUserList(new User());
        //分页数据
        PageUtils pageUtils = new PageUtils(userList, page, limit);
        List records = pageUtils.getRecords();
        //分页结束
        LayResult layResult = new LayResult(userList.size());
        return layResult.put(LayResult.DATA_TAG,records);
    }

    @PostMapping("/editConfirm")
    @ResponseBody
    public LayResult editConfirm(User user){
        int updateMessage = userService.updateUser(user);
        if(updateMessage == 0){
            System.out.println("更新失败");
            return new LayResult(500);
        }else return new LayResult();
    }

    @PostMapping("/addConfirm")
    @ResponseBody
    public LayResult addConfirm(User user){
        System.out.println(user);
        int updateMessage = userService.insertUser(user);
        if(updateMessage == 0){
            System.out.println("插入失败");
            return new LayResult(500);
        }else return new LayResult();
    }

}
